package com.hudongwx.godoffood.activity.Shopping_Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.utils.DBUtils.Address;
import com.hudongwx.godoffood.utils.DBUtils.DbOpenUtils;
import com.hudongwx.godoffood.wheelview.ChangeAddressPopwindow;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

@ContentView(R.layout.activity_address)
public class AddressActivity extends AppCompatActivity {
    @ViewInject(R.id.address_tv)
    TextView textView;
    @ViewInject(R.id.address_tv_1)
    TextView textView1;
    @ViewInject(R.id.address_ed_1)
    EditText editText1;
    @ViewInject(R.id.address_ed_2)
    EditText editText2;
    @ViewInject(R.id.address_ed_3)
    EditText editText3;
    @ViewInject(R.id.address_ed_4)
    EditText editText4;
    String TAG = "AddressActivity";
    String name = null;
    String text = null;
    String call = null;
    String zip = null;
    DbManager dbManager;
    ChangeAddressPopwindow mChangeAddressPopwindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        dbManager = DbOpenUtils.getShoppingDb2();
        Intent intent = getIntent();
        if (intent != null) {
            String name = intent.getStringExtra("name");
            String address = intent.getStringExtra("address");
            String call = intent.getStringExtra("call");
            String zip = intent.getStringExtra("zip");
            String address_1 = intent.getStringExtra("address_1");
            Log.i(TAG, "onCreate: "+name+address+call+zip+address_1);
            textView1.setText(address_1);
            editText1.setText(name);
            editText2.setText(call);
            editText3.setText(address);
            editText4.setText(zip);
        }
    }
//地址的自定义的popwindow
    @Event(R.id.address_relativeLayout_1)
    private void address(View view) {
        mChangeAddressPopwindow = new ChangeAddressPopwindow(AddressActivity.this);
        mChangeAddressPopwindow.setAddress("广东", "深圳", "福田区");
        //在哪里显示
        mChangeAddressPopwindow.showAtLocation(textView, Gravity.BOTTOM, 0, 0);
        mChangeAddressPopwindow.setAddresskListener(new ChangeAddressPopwindow.OnAddressCListener() {

            @Override
            public void onClick(String province, String city, String area) {
                // TODO Auto-generated method stub
                Toast.makeText(AddressActivity.this,
                        province + "-" + city + "-" + area,
                        Toast.LENGTH_LONG).show();
                textView1.setText(province + city + area);
            }
        });
    }

    @Event(R.id.address_img_2)
    private void address_img_2(View view) {
        finish();

    }

    /**
     * 往数据库中添加数据
     * @param view
     */
    @Event(R.id.address_bu)
    private void address_bu(View view) {

        Intent intent = new Intent(AddressActivity.this, NumberActivity.class);
        name = String.valueOf(editText1.getText());
        intent.putExtra("name", name);
        call = String.valueOf(editText2.getText());
        intent.putExtra("call", call);
        String text1 = String.valueOf(editText3.getText());
        text = String.valueOf(textView1.getText());
        intent.putExtra("address", text);
        intent.putExtra("address_1", text1);
        zip = String.valueOf(editText4.getText());
        intent.putExtra("zip", zip);
        setResult(0, intent);
        Log.i(TAG, "address_bu: " + name + call + text + zip + text1);
        try {

            dbManager.save(new Address(name, call, text, text1, zip, false));

            List<Address> all = dbManager.findAll(Address.class);
            for (int i = 0; i < all.size(); i++) {
                Log.i(TAG, "address_bu:gvhgg " + all.get(i).getAddress_2());
            }

        } catch (DbException e) {
            e.printStackTrace();
        }

        finish();
    }
}
